<template>
  <div>
    <li v-for="(item, index) in list" :key="index">
      <img :src="item.productImage" alt="" />
      <h4>{{ item.productName }}</h4>
      <p>{{ item.salePrice }}</p>
      <div @click="add(item)">加入购物车</div>
    </li>
    <el-dialog title="提示" :visible.sync="flag" width="40%">
      <span>加入购物车成功</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="flag = false">继续加入商品</el-button>
        <el-button type="danger" @click="toCar">查看购物车</el-button>
      </span>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="show" width="40%">
      <span>请先登录</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="show = false">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["list"],
  data() {
    return {
      flag: false,
      show: false,
    };
  },
  methods: {
    add(item) {
      if (localStorage.getItem("token")) {
        this.flag = true;
        this.$store.commit("add", item);
      } else {
        this.show = true;
      }
    },
    toCar() {
      this.$router.push({ path: "/about" });
    },
  },
};
</script>